package com.csoracle.manager.web;

import com.csoracle.beans.CheckItem;
import com.csoracle.beans.PageBean;
import com.csoracle.beans.QueryPageVo;
import com.csoracle.beans.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/manager/checkItem")
@Slf4j
public class CheckItemController {
    String baseUrl = "http://localhost:7001/";


    @Autowired
    RestTemplate restTemplate;

    @GetMapping("findList")
    public ResultInfo findList(QueryPageVo vo){
        System.out.println(vo);
//        springMVC中
        PageBean<CheckItem> pageBean = restTemplate.getForObject(baseUrl + "checkItem/findList?currentPage={currentPage}&pageSize={pageSize}&queryString={queryString}",PageBean.class,
                new Object[]{vo.getCurrentPage(),vo.getPageSize(),vo.getQueryString()});

        return new ResultInfo(true,"", pageBean);
    }

    @PostMapping("add")
    public ResultInfo add(@RequestBody CheckItem checkItem){
        log.warn(checkItem.toString());
        ResultInfo info = restTemplate.postForObject(baseUrl + "checkItem/add", checkItem, ResultInfo.class);

        return info;
    }

    @PutMapping("update")
    public ResultInfo update(@RequestBody CheckItem checkItem){
        log.warn(checkItem.toString());
        try {
            restTemplate.put(baseUrl + "checkItem/update", checkItem);
            return new ResultInfo(true,"修改成功", null);
        } catch (RestClientException e) {
            e.printStackTrace();
            return new ResultInfo(false,"修改失败", null);
        }


    }
}
